$(document).ready(function(){  
	//日期函数
	$('.dateInput').datetimepicker({
			format: 'yyyy-mm-dd',
	        forceParse: 0,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        minView: 2,
	        bootcssVer:3
	});
	$('.dateInput').datetimepicker('setStartDate', '1900-01-01');
	var endDate = new Date();
	$('.dateInput').datetimepicker('setEndDate', endDate);
	$("#functionNav").attr("href",navAddr);
	//设置上传影像的格式
	$("#yxImage").fileinput({	
		showUpload : false,
        showRemove : false,
        language : 'zh',
        allowedPreviewTypes : [ 'image' ],
        allowedFileExtensions : [ 'jpg' ],
	    overwriteInitial: true,
	    maxFileSize: 10240	     
	});
	//获取并填充拍摄方向
	var psfxJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInjb.do",{'tableName':'jb_psfx'});
	loadSelect("#form_yx select[name=psfx]",psfxJson.rows,'id','name');
	//获取并填充摄影师
	var sysJson = ajaxResult("get",false,ProjectName+"/root.getSheyingshi.do",{'roleId':'sheyingshi'});
	if(sysJson.status=="ok"){
		loadSelect("#form_yx select[name=psrid]",sysJson.rows,'userid','username');
	}
	//获取Url中的参数
    var cpid =GetQueryString("id");
    //获取藏品编号，并设置各个表单中的藏品编号
    $("input[name=cpbh]").val(cpid);
    //获取附属物影像列表
    createTable('#yxTable',ProjectName+'/colmanage.getYingxiangList.do','id',yxTblTitle,{'cpbh':cpid});
    
  //增加/修改影像信息
    $("#yx_btn").click(function(){
 	   if(checkform("#form_yx")){
 		  if($("#form_yx select[name=psrid]").val()!=null && $("#form_yx select[name=psrid]").val()!=''){
			   var psrmc =$("#form_yx select[name=psrid]").find("option:selected").text();
			   $("#form_yx input[name=psrmc]").val(psrmc);
		   }else{
			   $("#form_yx input[name=psrmc]").val('');
		   }		   
 		 if($("#form_yx input[name=id]").val()==null || $("#form_yx input[name=id]").val()==''){ 	   
 		   if($("input[name=yxImage]").val()!=null && $("input[name=yxImage]").val()!=''){
 			  $(".file-input").next("span").remove();
 		     var option = {
 			     url : ProjectName+"/colmanage.addDbYx.do",
 			     async:false,
 			     type: "post",
 			     dataType: 'json',
 			     success : function(data){	
 			    	if(data.status=="ok"){
 			    		 alertFunc("增加影像信息成功！");
			    	 }else{
			    		 alertFunc(data.error);
			    	 }
 				 }
 			  };
 			  $("#form_yx").ajaxSubmit(option);
 			  $('#yxModal').modal('hide');
 			  $("#form_yx")[0].reset();
 			  $('#yxTable').bootstrapTable('refresh');
 			  return false;
 		   }else{
 			   $("#form_yx .file-input").parent("div").after("<span class='apendhtml' style='color: red;margin-left: 5px;'>请选择文件</span>");
 			  
 		   }	
 		 }else{
 			   add_upload_data("#form_yx","/colmanage.addDbYx.do","影像","yx");
 			   $('#yxModal').modal('hide');
 		 }  
 	  }
    });
})

//点击增加按钮，改变模态框中保存按钮的值
function modal_add(tabname){
	$("input[name=id]").val('');
	$("input[name=opType]").val('add');
	$("#yxwjdiv").show();
	$('#yxViewDiv').hide();
	$("#form_"+tabname)[0].reset();	
	$("#"+tabname+"_btn").text("增加");
}

//增加/修改数据
function add_upload_data(formid,url,alertinfo,tblname){
	var params =$(formid).serializeObject();
	   var result="";
	   if(params.id == null || params.id == ""){
		   params['opType'] = "add";
		   result = ajaxResult("post",false,ProjectName+url,params);
		   if(result["status"] == 'ok'){
			   alertFunc("增加"+alertinfo+"信息成功！");
		   }else{
			   alertFunc(result["error"]);
		   }
	   }else{
		   params['opType'] = "update";
		   result = ajaxResult("post",false,ProjectName+url,params);
		   if(result["status"] == 'ok'){
			   alertFunc("修改"+alertinfo+"信息成功！");
		   }else{
			   alertFunc(result["error"]);
		   }
	   }	  	 	
	   $(formid)[0].reset();
	   $("input[name=id]").val('');
	   $("input[name=cpbh]").val('');
	   $('#'+tblname+'Modal').modal('hide');
	   $("#"+tblname+'Table').bootstrapTable('refresh');	
}

//填充影像修改信息
function modiYx(yxInfo){
	if (yxInfo != null){
		loadData("#form_yx",yxInfo);
		$('#form_yx input[name=opType]').val('update');
		$("#yxwjdiv").hide();
		$('#yxViewDiv img').attr('src',imageAddr+yxInfo.sldz);
		$('#yxViewDiv').show();
		$('#yxModal').modal('show');
		$("#yx_btn").text("修改");
	}
}

//删除影像
function delImg(id,yxdz,sldz,lldz){
	bootbox.setLocale("zh_CN");
	bootbox.confirm("确认要删除该影像文件么？",function(r){
		if(r){
			var params = new Object();
			params["id"]=id;
			params["yxdz"]=yxdz;
			params["sldz"]=sldz;
			params["lldz"]=lldz;
			var delyxJson = ajaxResult("get",false,ProjectName+"/colmanage.deleteDbYx.do",params);
			if(delyxJson.status=='ok'){
				$("#yxTable").bootstrapTable('refresh');
			}else{
				alertFunc(delyxJson.error);
			}	
		}
	});
}

//影像Title
var yxTblTitle = [{
    field: 'sldz',
    title: '缩略影像',
    formatter: function(value, row, index) {     	
    	if(value!="" && value!=null)	
      	    return "<img src='"+imageAddr+value+"'>";    	
    	else
    		return "";
    }
}, {
    field: 'psfxms',
    title: '拍摄方向'
}, {
    field: 'psrmc',
    title: '拍摄人'
},{
    field: 'pssj',
    title: '拍摄时间'
},{
    field: 'sfyx',
    title: '代表影像',
    formatter: function(value, row, index) {     	
    	if(value==1)	
      	    return "是";
    	else
    		return "";
      	}
},{
    field: '',
    title: '操作',
    formatter: function(value, row, index) {     
    	var result ="";
    	if(row.wcbz==0)		
	   	    result = "<a onclick='modiYx("+JSON.stringify(row)+")' href='javascript:void(0)'>修改</a>&nbsp;&nbsp;&nbsp;<a onclick='delImg("+row.id+",\""+row.yxdz+"\",\""+row.sldz+"\",\""+row.lldz+"\")' href='javascript:void(0)'>删除</a>";
    	return result;  
    } 	
}]